package com.swt.gap.controller;


import com.swt.gap.common.PageResult;
import com.swt.gap.model.entity.Task;
import com.swt.gap.model.vo.*;
import com.swt.gap.service.FileService;
import com.swt.gap.service.TaskFileService;
import com.swt.gap.service.TaskService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "数据资源相关")
@RestController
@RequestMapping("/task/file")
@Slf4j
public class TaskFileController {

    @Autowired
    private FileService fileService;

    @Autowired
    private TaskFileService taskFileService;

    @GetMapping
    @ApiOperation(value = "任务数据列表", notes = "任务数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "taskId", dataType = "int", value = "任务id"),
            @ApiImplicitParam(paramType = "query", name = "keyword", dataType = "String", value = "输入的搜索条件, 无搜索时不传此参数"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "int", value = "当前页"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", value = "每页的条数")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = PageResult.class)})
    public ResponseEntity listTaskFile(@RequestParam Integer taskId, @RequestParam String keyword,
                                       @RequestParam Integer pageNum, @RequestParam Integer pageSize){

        //  返回 List<TaskFileVO>
        return ResponseEntity.ok(fileService.getTaskFileByKey(taskId, keyword, pageNum, pageSize));
    }


    @GetMapping("typeFile/{taskId}")
    @ApiOperation(value = "任务数据分类统计", notes = "任务数据分类统计")
    @ApiImplicitParam(name = "taskId", dataType = "int",paramType = "path", value = "任务Id")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = TypeFileVO.class, responseContainer = "List")})
    public ResponseEntity getFileCountByType(@PathVariable Integer taskId){
        return ResponseEntity.ok(taskFileService.getTypeFileVOList(taskId));
    }


//    @PostMapping
//    @ApiOperation(value = "完成创建任务和数据关联", notes = "完成创建任务和数据关联")
//    @ApiImplicitParams ({@ApiImplicitParam(name = "taskFiles", dataType = "TaskFileAddVO", paramType = "body", value = "任务和数据信息")
//    })
//    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = Integer.class)})
//    public ResponseEntity saveTaskAndFile(@RequestBody TaskFileAddVO taskFiles) {
//
//        // 任务信息保存在task表里,基本信息保存在 file表   任务关联信息保存在task_file
//        return ResponseEntity.ok(taskFileService.completeTaskAndFile(taskFiles));
//    }


    @PostMapping("/{taskId}")
    @ApiOperation(value = "引入数据", notes = "引入数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", dataType = "int",paramType = "path", value = "任务Id"),
            @ApiImplicitParam(name = "fileIds", dataType = "array", paramType = "query", value = "引入的数据id集合")})
    @ApiResponses({@ApiResponse(code = 201, message = "引入成功")})
    public ResponseEntity addTaskFiles(@PathVariable Integer taskId, @RequestParam List<Integer> fileIds){
        taskFileService.addTaskFiles(taskId, fileIds);
        return ResponseEntity.status(HttpStatus.CREATED).body("引入成功");
    }

    @DeleteMapping("/{fileId}")
    @ApiOperation(value = "移除数据", notes = "移除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileId", dataType = "int",paramType = "path", value = "数据Id"),
            @ApiImplicitParam(name = "taskId", dataType = "int", paramType = "query", value = "任务Id")})
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    public ResponseEntity deleteTaskFile(@PathVariable Integer fileId, @RequestParam Integer taskId){
        taskFileService.deleteTaskFile(fileId, taskId);
        return ResponseEntity.ok("移除成功");
    }


    @GetMapping("file")
    @ApiOperation(value = "获取数据", notes = "获取数据")
    @ApiImplicitParam(paramType = "query", name = "keyword", dataType = "String", value = "输入的搜索条件, 无搜索时不传此参数")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = FileVO.class, responseContainer = "List")})
    public ResponseEntity getFile(@RequestParam String keyword){
        return ResponseEntity.ok(taskFileService.getFile(keyword));
    }

}
